package com.intelligencesoft.pss.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.intelligencesoft.pss.auth.LoginUtil;
import com.intelligencesoft.pss.controller.helper.BindingAuthorHelper;
import com.intelligencesoft.pss.entity.base.UserInfo;
import com.intelligencesoft.pss.entity.binding.UserBinding;
import com.intelligencesoft.pss.enums.BindingType;
import com.intelligencesoft.pss.service.binding.UserBindingService;
import com.intelligencesoft.pss.sina.weibo.weibo4j.Oauth;
import com.intelligencesoft.pss.sina.weibo.weibo4j.model.Result;
import com.intelligencesoft.pss.sina.weibo.weibo4j.model.WeiboException;

@Controller
@RequestMapping(value="/bindingAuthor")
public class BindingAuthorController {
	
	@Autowired
	private LoginUtil loginUtil;
	
	@Autowired
	private BindingAuthorHelper bindingAuthorHelper;
	
	@Autowired
	private Oauth oauth;
	
	@Autowired
	private UserBindingService userBindingService;
	
	/**
	 * 授权
	 * @author 黄莹璐
	 * @date: 2014-9-7-上午12:11:54
	 * //TODO  黄莹璐编辑
	 * @return
	 */
	@RequestMapping(value="/author",method=RequestMethod.GET)
	@ResponseBody
	public ModelMap author(BindingType bindingType){
		ModelMap modelMap = new ModelMap();
		String url = "";
		try {
			url = bindingAuthorHelper.getAuthorUrl(bindingType);
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		modelMap.put("url", url);
		return modelMap;
	}
	
	/**
	 * 取消授权
	 * @author 黄莹璐
	 * @date: 2014-9-7-上午12:12:05
	 * //TODO  黄莹璐编辑
	 * @return
	 */
	@RequestMapping(value="/cancelAuthor",method=RequestMethod.GET)
	@ResponseBody
	public ModelMap cancelAuthor(BindingType bindingType){
		ModelMap modelMap = new ModelMap();
		
		UserInfo userInfo = loginUtil.getLoginUser();
		UserBinding userBinding = userBindingService.findUserBindsByUserIdAndType(userInfo.getId(), bindingType);
		String tokenId = userBinding.getAccessToken();
		try {
			Result result = oauth.cancelAuthor(tokenId);
			modelMap.put("result", result);
			modelMap.put("userBinding", userBinding);
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return modelMap;
	}
	
	@RequestMapping(value="/cancelCallBack",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView cancelCallBack(){
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName("redirect:/home/showBindingList");
		
		return modelAndView;
	}
	
	@RequestMapping(value="/deleteUserBinding",method=RequestMethod.GET)
	@ResponseBody
	public ModelMap deleteUserBinding(String id){
		ModelMap modelMap = new ModelMap();
		try {
			userBindingService.deleteById(id);
			modelMap.put("result", true);
		} catch (Exception e) {
			modelMap.put("result", false);
		}
		return modelMap;
		
	}
	
	@RequestMapping(value="/getBindUserInfo",method=RequestMethod.GET)
	@ResponseBody
	public ModelMap getBindUserInfo(BindingType bindingType){
		ModelMap modelMap = new ModelMap();
		UserInfo userInfo = loginUtil.getLoginUser();
		
		if(userInfo != null){
			switch (bindingType) {
			case BINDING_SINA_WEIBO:
				
				break;

			default:
				break;
			}
		}
		modelMap.put("bindInfo", "");
		return modelMap;
	}
	
//	@RequestMapping(value="/getBindUserInfo",method=RequestMethod.GET)
//	@ResponseBody
//	public ModelMap bindReturn(BindingType bindingType,String code,String openid,String openkey){
//		
//	}
//	
//	private getSinaWeiBoInfo(){
//		
//	}
}
